/*
    This file is part of Slydini.

    Slydini is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Slydini is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Slydini.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "hand_tracker.h"


HandTracker::HandTracker(int clickThreshold) {
	updates = 0;
	this->clickThreshold = clickThreshold;
	bezier = Bezier::getInstance();
}


void HandTracker::update(Hand hand) {
	trackedHand.position.x = trackedHand.position.y = 0;
	trackedHand.confirmed = false;
	int fingers_active = 0;
	if (hand[0].ACTIVE)
	   fingers_active++;
   	if (hand[1].ACTIVE)
	   fingers_active++;
	if (fingers_active==1)
	   trackedHand.state.clicked = true;
	else
		trackedHand.state.clicked = false;
	

	if (hand[0].ACTIVE) {
		trackedHand.confirmed = true;
		trackedHand.position = hand[0].TIP.center;
	}
	if (hand[1].ACTIVE) {
		trackedHand.confirmed = true;
		trackedHand.position.x += hand[0].TIP.center.x;
		trackedHand.position.y += hand[0].TIP.center.y;
		if (hand[0].ACTIVE) {
			trackedHand.position.x /= 2;
			trackedHand.position.y /= 2;
			//if (abs(hand[0].TIP.center.y-hand[1].TIP.center.y) < clickThreshold)
			//	trackedHand.state.clicked = false;
			//else
			//	trackedHand.state.clicked = true;
		}

	}
	bezier->update(trackedHand.position);
	if (bezier->isValid())
		trackedHand.position = bezier->getBezierPosition();
	updates++;
}

bool HandTracker::handDetected() {
	return trackedHand.confirmed;
}

TrackedHand HandTracker::getTrackedHand() {
	return trackedHand;
}
